#include "stdafx.h"
#include "ASN1OctectString.h"

namespace AppliedCryptography
{
	ASN1OctectString::ASN1OctectString()
		: ASN1Encodable(Universal, Primitive, OctetString)
	{
	};

	ASN1OctectString::ASN1OctectString(const char* value)
		: ASN1Encodable(Universal, Primitive, OctetString)
	{
		for(unsigned int i=0; i < strlen(value); i++)
		{
			_value.push_back(value[i]);
		};
	};

	ASN1OctectString::ASN1OctectString(const byte* value, int length)
		: ASN1Encodable(Universal, Primitive, OctetString),
		_value(value, value + length)
	{
	};

	ASN1OctectString::ASN1OctectString(const std::vector<byte>& value)
		: ASN1Encodable(Universal, Primitive, OctetString),
		_value(value)
	{
	};

	ASN1OctectString::ASN1OctectString(const std::string& value)
		: ASN1Encodable(Universal, Primitive, OctetString),
		_value(value.c_str(), value.c_str() + value.size())
	{
	};

	ASN1OctectString& ASN1OctectString::set(const byte* value, int length)
	{
		_value.clear();
		for(int i=0; i<length; ++i)
		{
			_value.push_back(value[i]);
		};

		return *this;
	};

	ASN1OctectString& ASN1OctectString::set(const std::vector<byte>& value)
	{
		_value.clear();
		_value = value;

		return *this;
	};

	ASN1OctectString& ASN1OctectString::set(const std::string& value)
	{
		_value.clear();
		for(int i=0; value.c_str()[i] != 0; ++i)
		{
			_value.push_back(value.c_str()[i]);
		};

		return *this;
	};

	ASN1OctectString& ASN1OctectString::operator=(const std::vector<byte>& value)
	{
		this->set(value);
		return *this;
	};

	ASN1OctectString& ASN1OctectString::operator=(const std::string& value)
	{
		this->set(value);
		return *this;
	};

	std::vector<byte> ASN1OctectString::get() const
	{
		return _value;
	};

	std::string ASN1OctectString::getString() const
	{
		std::string ret;

		for(unsigned int i=0; i<_value.size(); i++)
		{
			ret.insert(i, 1, _value[i]);
		};

		return ret;
	};

	std::vector<byte> ASN1OctectString::getContentBits() const
	{
		std::vector<byte> ret;
		ret.insert(ret.end(), _value.begin(), _value.end());
		return ret;
	};

	unsigned int ASN1OctectString::size() const
	{
		return _value.size();
	};
}; // namespace AppliedCryptography
